package acm.蓝桥1;

import java.util.*;
/**
 * https://www.dotcpp.com/oj/problem1426.html
 */
public class 九宫重拍 {
    static int[][] src = new int[3][3];
    static int[][] target = new int[3][3];
    static int sx, sy;
    static int tx, ty;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int n = 0;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                src[i][j] = s.charAt(n++);
                if (src[i][j] == '.') {
                    sx = i;
                    sy = j;
                    src[i][j] = 9;
                } else {
                    src[i][j] -= '0';
                }
            }
        }
        String t = sc.nextLine();
        n = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                target[i][j] = t.charAt(n++);
                if (target[i][j] == '.') {
                    tx = i;
                    ty = j;
                    target[i][j] = 9;
                } else {
                    target[i][j] -= '0';
                }
            }
        }
        System.out.println(bfs());
    }


    public static int bfs() {
        Node start = new Node(src, sx, sy);
        Node end = new Node(target, tx, ty);

        Set<Node> set = new HashSet<>();  //存储走过的点
        Queue<Node> queue = new ArrayDeque<>();
        queue.add(start);
        set.add(start);
        int step = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node poll = queue.poll();
                if (poll.equals(end)) {
                    return step;
                }
                if (poll.hasLeft()) {
                    Node left = poll.left();
                    if (!set.contains(left)) {
                        queue.add(left);
                        set.add(left);
                    }
                }
                if (poll.hasRight()) {
                    Node right = poll.right();
                    if (!set.contains(right)) {
                        queue.add(right);
                        set.add(right);
                    }
                }
                if (poll.hasUp()) {
                    Node up = poll.up();
                    if (!set.contains(up)) {
                        queue.add(up);
                        set.add(up);
                    }
                }
                if (poll.hasDown()) {
                    Node down = poll.down();
                    if (!set.contains(down)) {
                        queue.add(down);
                        set.add(down);
                    }
                }
            }
            step++;
        }
        return -1;
    }

    static class Node {
        int[][] data;
        int x, y;
        int hashCode;

        public Node(int[][] data, int x, int y) {
            this.data = data;
            this.x = x;
            this.y = y;
            for (int[] datum : data) {
                for (int c : datum) {
                    hashCode *= 10;
                    hashCode += c;
                }
            }
        }
        public boolean hasLeft(){
            return y>0;
        }
        public boolean hasRight(){
            return y<2;
        }
        public boolean hasUp(){
            return x>0;
        }
        public boolean hasDown(){
            return x<2;
        }
        public Node left(){
            int[][] copy = clone(data);
            int t = copy[x][y];
            copy[x][y] = copy[x][y - 1];
            copy[x][y - 1] = t;
            return new Node(copy, x, y - 1);
        }
        public Node right(){
            int[][] copy = clone(data);
            int t = copy[x][y];
            copy[x][y] = copy[x][y + 1];
            copy[x][y + 1] = t;
            return new Node(copy, x, y + 1);
        }
        public Node up(){
            int[][] copy = clone(data);
            int t = copy[x][y];
            copy[x][y] = copy[x - 1][y];
            copy[x - 1][y] = t;
            return new Node(copy, x - 1, y);
        }
        public Node down(){
            int[][] copy = clone(data);
            int t = copy[x][y];
            copy[x][y] = copy[x + 1][y];
            copy[x + 1][y] = t;
            return new Node(copy, x + 1, y);
        }

        public static int[][] clone(int[][] arr) {
            int[][] copy = new int[3][3];
            for (int i = 0; i < 3; i++) {
                copy[i]=arr[i].clone();
            }
            return copy;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return hashCode==node.hashCode;
        }

        @Override
        public int hashCode() {
            return hashCode;
        }
    }
}
